package cn.janescott.eurekaclient;

import com.netflix.discovery.EurekaClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * eureka client
 *
 * @author scott
 */
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(EurekaClientApplication.class, args);
        /* 系统关闭时，自动注销服务 */
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                System.out.println("closing...");
                EurekaClient bean = context.getBean(EurekaClient.class);
                bean.shutdown();
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }));

        /* 测试10s之后自动注销服务 */
//        IntStream.range(0, 10).forEachOrdered(
//                it -> {
//                    try {
//                        System.out.println("counting...");
//                        Thread.sleep(1000);
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                    }
//                }
//        );
//        System.out.println("closing...");
//        EurekaClient bean = context.getBean(EurekaClient.class);
//        bean.shutdown();
    }

}

